/*
 * Copyright (C) 2009 Leandro Aparecido <lehphyro@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.metalframework.persistence;

import java.util.*;

import org.springframework.dao.*;

import com.google.code.liquidform.*;

/**
 * Represents the entire extent of all instances of an entity type in the system.
 * 
 * @param <T> Entity type of the extent.
 * @since 1.0
 */
public interface Extent<T> extends Set<T> {
	/**
	 * Adds an entity instance in the extent.
	 * 
	 * @param <K> Type of the returned identifier of the entity.
	 * @param entity Entity to add to the extent.
	 * @return Identifier of the newly added entity.
	 */
	<K> K addInstance(T entity);
	
	/**
	 * Updates an entity instance in the extent.
	 * 
	 * @param entity Entity instance to update with its identifier already set.
	 */
	void set(T entity);

	/**
	 * Updates an entity instance in the extent.
	 * 
	 * @param id Identifier of the entity to update.
	 * @param entity Entity instance to update.
	 */
	void set(Object id, T entity);

	/**
	 * Retrieves an entity instance using its id.
	 * 
	 * @param id Id of the entity to retrieve.
	 * @return Entity instance retrieved or null if not found.
	 */
	T byId(Object id);

	/**
	 * Retrieves an entity instance using its id.
	 * 
	 * @param <E> Exception type.
	 * @param id Id of the entity to retrieve.
	 * @param exception Exception class to be thrown if entity not found.
	 * @return Entity instance retrieved.
	 * @throws E Thrown if entity not found.
	 */
	<E extends Throwable> T byId(Object id, Class<E> exception) throws E;

	/**
	 * Query the repository using a custom LIQUidFORM clause.
	 * 
	 * @param clause LIQUidFORM clause.
	 * @param params Positional parameters for the JPA query.
	 * @return List of entities matching the JPA query clause.
	 */
	List<T> query(SubQuery<T> clause, Object... params);

	/**
	 * Query the repository using a custom LIQUidFORM clause.
	 * 
	 * @param clause LIQUidFORM clause.
	 * @param params Positional parameters for the JPA query.
	 * @return List of entities matching the JPA query clause.
	 */
	List<T> query(SelectClause<T> clause, Object... params);

	/**
	 * Query the repository using a custom LIQUidFORM clause.
	 * 
	 * @param clause LIQUidFORM clause.
	 * @param params Positional parameters for the JPA query.
	 * @return List of entities matching the JPA query clause.
	 */
	List<T> query(OrderByClause clause, Object... params);

	/**
	 * Query the repository using a custom LIQUidFORM clause.
	 * 
	 * @param clause LIQUidFORM clause.
	 * @param params Positional parameters for the JPA query.
	 * @return Single entity matching the JPA query clause or
	 * {@link IncorrectResultSizeDataAccessException} if resulting list is empty or
	 * has more than one element.
	 */
	T queryForSingleInstance(SubQuery<T> clause, Object... params);

	/**
	 * Query the repository using a custom LIQUidFORM clause.
	 * 
	 * @param <E> Exception type to be thrown if the result list does not have
	 * exactly one element.
	 * @param clause LIQUidFORM clause.
	 * @param exception Type of the exception to be thrown if the resulting list is
	 * empty or has more than a single element.
	 * @param params Positional parameters for the JPA query.
	 * @return Single entity matching the JPA query clause or
	 * an instance of the specified exception if resulting list is empty or
	 * has more than a single element.
	 */
	<E extends Throwable> T queryForSingleInstance(SubQuery<T> clause, Class<E> exception, Object... params) throws E;

	/**
	 * Query the repository using a custom LIQUidFORM clause.
	 * 
	 * @param clause LIQUidFORM clause.
	 * @param params Positional parameters for the JPA query.
	 * @return Single entity matching the JPA query clause or
	 * {@link IncorrectResultSizeDataAccessException} if resulting list is empty or
	 * has more than a single element.
	 */
	T queryForSingleInstance(SelectClause<T> clause, Object... params);

	/**
	 * Query the repository using a custom LIQUidFORM clause.
	 * 
	 * @param <E> Exception type to be thrown if the result list does not have
	 * exactly one element.
	 * @param clause LIQUidFORM clause.
	 * @param exception Type of the exception to be thrown if the resulting list is
	 * empty or has more than a single element.
	 * @param params Positional parameters for the JPA query.
	 * @return Single entity matching the JPA query clause or
	 * an instance of the specified exception if resulting list is empty or
	 * has more than a single element.
	 */
	<E extends Throwable> T queryForSingleInstance(SelectClause<T> clause, Class<E> exception, Object... params) throws E;

	/**
	 * Query the repository using a custom LIQUidFORM clause.
	 * 
	 * @param clause LIQUidFORM clause.
	 * @param params Positional parameters for the JPA query.
	 * @return Single entity matching the JPA query clause or
	 * {@link IncorrectResultSizeDataAccessException} if resulting list is empty or
	 * has more than a single element.
	 */
	T queryForSingleInstance(OrderByClause clause, Object... params);

	/**
	 * Query the repository using a custom LIQUidFORM clause.
	 * 
	 * @param <E> Exception type to be thrown if the result list does not have
	 * exactly one element.
	 * @param clause LIQUidFORM clause.
	 * @param exception Type of the exception to be thrown if the resulting list is
	 * empty or has more than a single element.
	 * @param params Positional parameters for the JPA query.
	 * @return Single entity matching the JPA query clause or
	 * an instance of the specified exception if resulting list is empty or
	 * has more than a single element.
	 */
	<E extends Throwable> T queryForSingleInstance(OrderByClause clause, Class<E> exception, Object... params) throws E;

	/**
	 * Query the repository using a custom JPA clause.
	 * 
	 * @param clause String clause.
	 * @param params Positional parameters for the JPA query.
	 * @return Single entity matching the JPA query clause or
	 * {@link IncorrectResultSizeDataAccessException} if resulting list is empty or
	 * has more than a single element.
	 */
	T queryForSingleInstance(String clause, Object... params);

	/**
	 * Query the repository using a custom JPA clause.
	 * 
	 * @param clause String clause.
	 * @param exception Type of the exception to be thrown if the resulting list is
	 * empty or has more than a single element.
	 * @param params Positional parameters for the JPA query.
	 * @return Single entity matching the JPA query clause or
	 * an instance of the specified exception if resulting list is empty or
	 * has more than a single element.
	 */
	<E extends Throwable> T queryForSingleInstance(String clause, Class<E> exception, Object... params) throws E;

	/**
	 * Query the repository using a custom JPA clause.
	 * 
	 * @param clause LIQUidFORM clause.
	 * @param params Positional parameters for the JPA query.
	 * @return List of entities matching the JPA query clause.
	 */
	List<T> query(String clause, Object... params);
}
